//
//  LMChangeSignController.m
//  舍品
//
//  Created by 楼 眉 on 16/3/24.
//  Copyright © 2016年 楼 眉. All rights reserved.
//
static CGFloat        const   KTextViewFontValue      =       15.0;
static NSInteger    const   KSignLabelNum                =        15;
#import "LMChangeSignController.h"
#import "LMWarningLabel.h"

@interface LMChangeSignController ()<UITextViewDelegate>

@property (nonatomic,strong)  UITextView *textView;

@end


static    NSString    *const    KUserInformationChange   =   @"userInformationChange";


@implementation LMChangeSignController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = NO;
    [self setupUi];
}
- (void)setupUi
{
    LMUser *user = [LMUser currentUser];
    self.view.backgroundColor  = [UIColor lightGrayColor];
    
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(20*kUnit, CGRectGetMaxY(self.navigationController.navigationBar.frame) + 20*kUnit, SCREEN_WIDTH - 40*kUnit, SCREEN_HEIGHT / 5.5)];
    self.textView = textView;
    textView.text = user.signString;
    textView.delegate = self;
    textView.font = [UIFont systemFontOfSize:KTextViewFontValue];
    textView.textColor = [UIColor lightGrayColor];
    [self.view addSubview:textView];
    [textView becomeFirstResponder];

    UILabel *warningLabel = [[UILabel alloc]init];
    [self.view addSubview:warningLabel];
    warningLabel.frame = CGRectMake(20*kUnit, CGRectGetMaxY(textView.frame) + 20*kUnit, SCREEN_WIDTH - 40*kUnit, 33*kUnit);
    warningLabel.textAlignment = NSTextAlignmentCenter;
    warningLabel.textColor = [UIColor whiteColor];
    warningLabel.text = [NSString stringWithFormat:@"最多输入%ld个字符",(long)KSignLabelNum];
    
    UIButton *btn = [[UIButton alloc]init];
    [self.view addSubview:btn];
    btn.backgroundColor = RGBCOLOR(39, 177, 235);
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    btn.frame = CGRectMake(textView.x, CGRectGetMaxY(warningLabel.frame) + 20*kUnit, textView.width, 44*kUnit);
    [btn addTarget:self action:@selector(saveSign) forControlEvents:UIControlEventTouchUpInside];
    
    
}
#pragma mark - 保存按钮点击
- (void)saveSign
{
    if ([AFNetworkReachabilityManager sharedManager].isReachable == NO) {
        [LMWarningLabel LM_createWarningLabelWithTitle:nil];
    }
    else
    {
        [SVProgressHUD showWithStatus:@"保存中..."];
        LMUser *user = [LMUser currentUser];
        NSString *oldSign = user.signString;
        user.signString = self.textView.text;
        __weak typeof(self) weakSelf = self;
        [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                [SVProgressHUD dismiss];
                [LMWarningLabel LM_createWarningLabelWithTitle:@"修改签名成功"];
                [weakSelf.navigationController popViewControllerAnimated:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KUserInformationChange object:nil];
            }
            else
            {
                [SVProgressHUD dismiss];
                weakSelf.textView.text = oldSign;
                user.signString = oldSign;
                [LMWarningLabel LM_createWarningLabelWithTitle:@"修改失败"];
            }
        }];
}
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (textView.text.length >= KSignLabelNum && range.length == 0) {
        return NO;
    }
    return YES;
}
#pragma mark - didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}
#pragma mark - dealloc
- (void)dealloc
{
    NSLog(@"dealloc--changeSignVc");
}
@end
